package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserFieldBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.dto.UserFieldConfigSelectBean;
import com.koron.css2.serviceManage.mapper.UserFieldConfigMapper;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
/**
 * 用戶字段配置列表
 * @author 甄玉晴
 *
 */
public class UserFieldConfigSelect  implements ServerInterface{
	
	private static Logger logger = LogManager.getLogger(UserFieldConfigSelect.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		
		@SuppressWarnings("rawtypes")
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
		try {	
			UserFieldBean bean = JsonUtils.objectToPojo(req.getData(), UserFieldBean.class);
			bean.setGroupCode(userInfo.getCurWaterCode());
			//获取mapper
			UserFieldConfigMapper mapper = factory.getMapper(UserFieldConfigMapper.class);
			//先根据页面获取分组信息
			List<String> groups=mapper.userFieldGroupSelect(bean);
			List<UserFieldConfigSelectBean> list=new ArrayList<UserFieldConfigSelectBean>();
			for(String group:groups) {
				bean.setGroup(group);
				//根据分组信息获取各个组的字段
                List<UserFieldBean> fields=mapper.userFieldConfigSelect(bean);
				if(fields!=null&&fields.size()!=0) {
					UserFieldConfigSelectBean config=new UserFieldConfigSelectBean();
					config.setGroup(group);
					config.setFields(fields);
					list.add(config);
				}
			}
			info.setData(list);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}


}
